package org.kalai.unifypay.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.mins.MinsPay;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.StringUtil;

import com.alibaba.fastjson.JSON;

/**
 * 真实商户注册(民生)
 * @author zsl-2016年9月26日
 *
 */
@WebServlet(urlPatterns = "/realMerchantRegister")
public class RealMerchantRegisterServlet extends HttpServlet {

   private static final long   serialVersionUID = 1L;

   private static final Logger logger           = Logger.getLogger(RealMerchantRegisterServlet.class);

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      CommonUtil.responseObject(resp, Constant.ERROR_REQEST);
   }

   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      Map<String, Object> result = CommonUtil.createRespMap("00", "商户注册成功");
      /* 获取应用参数 */
      Map<String, String> param = CommonUtil.validate(request, response);
      if (param == null) {
         logger.error("系统参数验证失败");
      }
      String merchantId = param.get("merchantId");// 商户号
      try {
         String reqMsgId = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
         String merchantName = param.get("merchantName");// 商户名称
         String shortName = param.get("shortName");// 商户简称
         String merchantAddress = param.get("merchantAddress");// 商户地址
         String servicePhone = param.get("servicePhone");// 客服电话
         String category = param.get("category");// 经营类目 详见 支付宝-全行业类目表.xls
         String type = param.get("type"); //类别1-支付宝;2-微信
         if (StringUtil.isEmpty(merchantId, merchantName, shortName, merchantAddress, servicePhone, category, type)) {
            logger.error(String.format(
                  "业务参数错误：merchantId=%s, merchantName=%s, shortName=%s, merchantAddress=%s, servicePhone=%s, category=%s",
                  merchantId, merchantName, shortName, merchantAddress, servicePhone, category));
            result = CommonUtil.createRespMap("200", "业务参数错误");
            return;
         }
         if(!type.equals("1")&&!type.equals("2"))
         {
            result = CommonUtil.createRespMap("200", "type业务参数错误");
            return;
         }
         result = MinsPay.merchantApply(Integer.valueOf(type), reqMsgId, merchantId, merchantName, shortName, merchantAddress,
               servicePhone, category);
         if (result.get("respCode").equals("00")) {
            // 真实商户登记成功后修改内部逻辑
            String merchantCode = result.get("merchantCode").toString();
            DataDao.getInstance().addAlipayMerchant(merchantId, merchantName, shortName, merchantAddress, servicePhone, category,
                  type, merchantCode, "1");
            if(!DataDao.getInstance().updateMemberBizOFmemberCode(merchantId,merchantCode,type))
            {
               result = CommonUtil.createRespMap("10", "真实商户登记异常请联系客服！");
               logger.error(String.format("[%s]商户业务修改失败:type=[%s]merchantName=[%s]result=[%s]", merchantId,type, merchantName, JSON.toJSONString(result)));
            }
         } else {
            logger.error(String.format("[%s]商户注册失败:type=[%s]merchantName=[%s]result=[%s]", merchantId,type, merchantName, JSON.toJSONString(result)));
         }
      } catch (Exception e) {
         String message = e.getMessage();
         if (StringUtil.isEmpty(message)) {
            result = CommonUtil.createRespMap("10", "系统异常请联系相关技术人员");
         } else {
            result = CommonUtil.createRespMap("10", e.getMessage());
         }
         logger.error(String.format("[%s]真实商户注册异常:%s", merchantId, e));
      } finally {
         CommonUtil.responseObject(response, JSON.toJSONString(result));
      }

   }
}
